package cn.kailangtu.commontools;

import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;

import java.io.File;

public class AppInstallUtil {

    private static String APK_NAME = "EPTON.apk";




    /***
     * 找U盘
     * @return
     */
    public static String findUsbPath()
    {
        String result = "";
        File file = new File("/storage/");
        if(file.exists())
        {
            if(file.listFiles() != null && file.listFiles().length>0)
            {
                for(File childFile : file.listFiles())
                {
                    String path = childFile.getPath().toLowerCase();
                    if(childFile.isDirectory() && path.indexOf("emulated") < 0 && path.indexOf("self") < 0)
                    {
                        result = childFile.getPath() +"/";
                        break;
                    }
                }
            }
        }
        return result;
    }


    /***
     * 找U盘
     * @return
     */
    public static String findUsbPath9()
    {
        String result = "";
        Log.d("SHOOTLEE_USB","查路径/mnt/media_rw/");
        File file = new File("/mnt/media_rw/");
        String cmd = "chmod 666 /mnt/media_rw/";
        AdbUtil.run(cmd);
        if(file.exists())
        {
            Log.d("SHOOTLEE_USB","查路径/mnt/media_rw/存在");
            if(file.listFiles() != null && file.listFiles().length>0)
            {
                Log.d("SHOOTLEE_USB","查路径/mnt/media_rw/存在，找到文件");
                for(File childFile : file.listFiles())
                {
                    String path = childFile.getPath();
                    Log.d("SHOOTLEE_USB","查路径/mnt/media_rw/存在，找到文件，"+path);
                        result = childFile.getPath() +"/";
                        Log.d("SHOOTLEE_USB","查路径/mnt/media_rw/存在，找到文件，找到结果"+result);



                        break;
                }
            }
        }
        return result;
    }


    private static void toInstance(String apkFile)
    {


            File usbFile = new File(apkFile);
            if(usbFile.exists())
            {
                Log.d("SHOOTLEEtoInstance","发现APK,正在安装："+apkFile);
                isInstallApk  =true;
                String cmd = "pm install "+apkFile;
                AdbUtil.run(cmd);
            }

    }

    static boolean isFindUsb = false;
    static boolean isInstallApk = false;
    public static void toSpy()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        String usbPath = findUsbPath();
                        if (usbPath != null && usbPath.length() > 0) {

                            Log.d("SHOOTLEE_USB","发现U盘：进来了");
                            String apkFullName = usbPath + APK_NAME;
                            isFindUsb = true;
                            if(!isInstallApk) {
                                isInstallApk = true;
                                String cmd = "am start -n cn.kailangtu.apkinstall/.MainActivity";
                                AdbUtil.run(cmd);
                            }
                        }
                        else{
                            usbPath = findUsbPath9();
                            if (usbPath != null && usbPath.length() > 0) {

                                Log.d("SHOOTLEE_USB","发现U盘：进来了");
                                String apkFullName = usbPath + APK_NAME;
                                isFindUsb = true;
                                if(!isInstallApk) {
                                    isInstallApk = true;
                                    String cmd = "am start -n cn.kailangtu.apkinstall/.MainActivity";
                                    AdbUtil.run(cmd);
                                    SystemClock.sleep(5000);
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                }
                            }
                        }


                        SystemClock.sleep(1000);
                    }
                }
                catch(Exception e)
                {}
            }
        }).start();
    }

}
